Page({
  data: {
    city: "加载中...",
    provice:'',
    temp: "--",
    icon:'',
    weather: "loading",
    realFeel: "--",
    humi:'34',
    indicate: [
      { title: "湿度", value: "--", unit: "%" },
      { title: "风速", value: "--", unit: "km/h" },
      { title: "气压", value: "--", unit: "hPa" },
      { title: "能见度", value: "--", unit: "km" }
    ],
    weekly: [
      { week: '周一',fxDate:'03/19', weather: 'cloudy', temp: 26 },
      { week: '周二',fxDate:'03/20', weather: 'rain', temp: 24 },
      { week: '周三',fxDate:'03/21', weather: 'sunny', temp: 28 },
      { week: '周四',fxDate:'03/22', weather: 'cloudy', temp: 25 },
      { week: '周五',fxDate:'03/23', weather: 'thunder', temp: 23 }
    ],
    lifeIndex: [
      { 
        name: '紫外线', 
        level: '中等', 
        desc: '建议佩戴太阳镜',
        image: '/images/weather/uv.png' 
      },
    ]
  },

  onLoad() {
    this.loadWeatherData();
  },


  // 更新数据加载方法
  // 在loadWeatherData方法中补充
  loadWeatherData() {
    const city = wx.getStorageSync('city') || {};
    const weather = wx.getStorageSync('weather') || {};
    const indicate = wx.getStorageSync('indicate') || {};
    const after_days = wx.getStorageSync('after_days') || {};
  // 处理未来天气数据
    const processedWeekly = after_days.slice(0, 5).map(item => {
    const dateObj = new Date(item.fxDate);
    const weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    return {
      ...item,
      week: weekDays[dateObj.getDay()],  // 添加周几字段
      fxDate: item.fxDate.substring(5), // 截取后5位 03-21
      temp: parseInt(item.tempMax)      // 保持现有温度字段
    };
    });
    // 处理天气指数
    const imagePaths = [
      '/images/run.jpeg',
      '/images/clother.jpeg', 
      '/images/zwx.jpeg',
      '/images/sf.jpeg'
    ];
    const processedLifeIndex = indicate.slice(0, 4).map((item, index) => ({
      ...item,
      image: imagePaths[index] // 按顺序添加图片字段
    }));
    //
    this.setData({
        city:city.adm2,
        provice:city.adm1,
        temp:weather.temp,
        icon:weather.icon,
        realFeel:weather.feelsLike,
        weather:weather.text,
        km:weather.vis,
        speed:weather.windSpeed,
        scle:weather.windDir,
        humi:weather.humi,
        weekly:processedWeekly,
        processedLifeIndex
    });
  },
  formatIndicators(rawData) {
    return this.data.indicate.map((item, index) => ({
      ...item,
      value: rawData?.[index]?.value || "--"
    }));
  }
});